import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 Created by IntelliJ IDEA.
 User: wk
 Date: 2017-03-28 下午5:51
 */
public class Test {
    public static void putShort(byte b[], short s, int index) {
        b[index + 1] = (byte) (s >> 8);
        b[index + 0] = (byte) (s >> 0);
    }
    public static void main(String args[]) {
        File file = new File("C:\\Windows\\xabtest.dll");
        try {

            FileChannel fileChannel = new RandomAccessFile(file, "rw").getChannel();
            byte[] dd = new byte[2];
            byte[] ddlong = new byte[8];
            putShort(dd,(short) 1,0);
            putLong(ddlong,(long)10000,0);
            ByteBuffer byteBuffer = ByteBuffer.allocate(10);
            byteBuffer.put(dd,0,2);
            byteBuffer.put(ddlong,0,8);
            byteBuffer.clear();
            fileChannel.write(byteBuffer,8);
            fileChannel.close();
//            fileChannel = new RandomAccessFile(file, "r").getChannel();
//            byteBuffer = ByteBuffer.allocate(10);
//            fileChannel.read(byteBuffer,8);
//            byte[] ddnew = new byte[2];
//            byteBuffer.get(ddnew);
//            System.out.println(byteBuffer.get());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static short getShort(byte[] b, int index) {
        return (short) (((b[index + 1] << 8) | b[index + 0] & 0xff));
    }

    public static void putLong(byte[] bb, long x, int index) {
        bb[index + 7] = (byte) (x >> 56);
        bb[index + 6] = (byte) (x >> 48);
        bb[index + 5] = (byte) (x >> 40);
        bb[index + 4] = (byte) (x >> 32);
        bb[index + 3] = (byte) (x >> 24);
        bb[index + 2] = (byte) (x >> 16);
        bb[index + 1] = (byte) (x >> 8);
        bb[index + 0] = (byte) (x >> 0);
    }
}
